# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=readline
pkgver=8.0.4
pkgrel=0
_myver=${pkgver%.*}
pkgdesc="GNU readline library"
url="https://tiswww.cwru.edu/php/chet/readline/rltop.html"
arch="all"
options="!check"
license="GPL-2.0-or-later"
makedepends_host="ncurses-dev chrpath"
subpackages="$pkgname-static $pkgname-dev $pkgname-doc libhistory"
source="https://ftp.gnu.org/gnu/readline/readline-$_myver.tar.gz
	fix-ncurses-underlinking.patch
	inputrc
	"

for _patch in $(seq -w 001 ${pkgver##*.}); do
	_name=$pkgname${_myver//./}-$_patch
	source="$source $_name.diff::https://ftp.gnu.org/gnu/$pkgname/$pkgname-$_myver-patches/$_name"
done

builddir="$srcdir/$pkgname-$_myver"

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--enable-static \
		--enable-shared
	make
}

package() {
	make -C "$builddir" DESTDIR="$pkgdir" install
	# verfy that its not underlinked as upstream designed it
	if ! readelf -d "$pkgdir"/usr/lib/libreadline.so | grep -q 'NEEDED.*ncurses'; then
		error "readline needs to be linked against ncurses"
		return 1
	fi
	rmdir "$pkgdir"/usr/bin
	mkdir -p "$pkgdir"/etc
	install -m644 "$srcdir"/inputrc "$pkgdir"/etc/inputrc
	chrpath -d "$pkgdir"/usr/lib/*.so.*
}

libhistory() {
	pkgdesc="GNU History Library"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/$subpkgname.* "$subpkgdir"/usr/lib
}

sha512sums="41759d27bc3a258fefd7f4ff3277fa6ab9c21abb7b160e1a75aa8eba547bd90b288514e76264bd94fb0172da8a4faa54aab2c07b68a0356918ecf7f1969e866f  readline-8.0.tar.gz
325dcf74e9f463a74fb116cb6f3ff8d9708dbec24b423a778eeda3a5ac4fe6df131e0e99d034053ad356b01502894ecc8facc09160d4c29b2291bd95cff6b635  fix-ncurses-underlinking.patch
5dbe872e94166aaed7ca2edec5a34ef9b13b254381e252cc6d851877b461579903cbb5b5dc588eabececcf1ebe636f6cb4da406cd01b64757f8c7e7f62e9a276  inputrc
a148cda6ac97935d51ede1bbacf847d7d2cba8861cf3941d32e3da2f45f16dcc05b20845b63d4264692520db8329e1236cf2bc4fd8ccbe6e40921bbbdeb2cb1c  readline80-001.diff
a941d142a59fe592cc45c1cf4d416e2fc0ab543dd6a0496fb8b2ec6f651cedd6fb9e0c105f14880700b69b2d6912653b3f66371e45cbe4adc0cc9554801afd3c  readline80-002.diff
85235fca047b4ac87b846d998fffc67ae67ed365b068fd2743e84f11fd00086f85e622955192e07e3560965e32ff5f6d014e0146cde528fb0d0f7b5b829875ca  readline80-003.diff
301ced2361e7583468f9932038936387a016c20d1c41966658a78c7f7c48c70308bf66b3228de0a3c6fc880427b6a90ee1ac4d2693daed432e5ae970671a7461  readline80-004.diff"
